package cn.com.pajx.pajx_smart_platform.ui.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import cn.com.pajx.pajx_smart_platform.R;
import cn.com.pajx.pajx_smart_platform.tools.LogUtil;
import cn.com.pajx.pajx_smart_platform.tools.ToastUtil;


/**
 * 自定义加载进度对话框
 * Created by ruancw on 2023/04/03.
 */

public class ExitDialog extends Dialog {

    private final Context context;

    public ExitDialog(Context context) {
        super(context);
        this.context = context;
        setContentView(R.layout.dialog_exit);
        ///设置背景透明，不然会出现白色直角问题
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        }
        setCanceledOnTouchOutside(false);
        initView();
    }

    private void initView() {
        //展示菜单的view
        TextView tvCancel = findViewById(R.id.tv_cancel);
        TextView tvConfirm = findViewById(R.id.tv_confirm);

        tvCancel.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    tvCancel.setBackgroundResource(R.drawable.shape_cancel_selector);
                    tvConfirm.setBackgroundResource(R.drawable.shape_confirm_normal);
                } else {
                    tvCancel.setBackgroundResource(R.drawable.shape_cancel_normal);
                }
            }
        });
        tvConfirm.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                    tvConfirm.setBackgroundResource(R.drawable.shape_confirm_selector);
                    tvCancel.setBackgroundResource(R.drawable.shape_cancel_normal);
                } else {
                    tvConfirm.setBackgroundResource(R.drawable.shape_confirm_normal);
                }
            }
        });

        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (mListener != null) {
                    LogUtil.e("退出");
                    ToastUtil.toast(context.getResources().getString(R.string.app_name) + "已退出");
                    mListener.popupChildView();
                }

            }
        });
    }

    private ViewClickListener mListener;

    public interface ViewClickListener {
        void popupChildView();
    }

    /**
     * 设置子View点击事件回调
     */
    public void setViewOnClickListener(ViewClickListener listener) {
        mListener = listener;
    }

}
